package cn.tude.boot07.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile pic) throws IOException {
        System.out.println("pic = " + pic);
        String fileName=pic.getOriginalFilename();
        System.out.println(fileName);
        String suffix=fileName.substring(fileName.lastIndexOf("."));
        fileName= UUID.randomUUID()+suffix;
        System.out.println(fileName);
        String dirPath="d:/files";
        File dirFile=new File(dirPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath=dirPath+"/"+fileName;
        pic.transferTo(new File(filePath));

        return "/"+fileName;
    }
    @RequestMapping("/remove")
    public void remove(String url){
        System.out.println("url = " + url);
        new File("d:/files"+url).delete();
    }
}
